// 登录
export const userLoginService = ({username,password}) => {
  wx.request({
    // url: 'http://106.53.70.21:2626/api/v2/login',
    url:'http://202.192.19.106:8080/api/v2/login',
    method:"POST",
    data : {
      username : username,
      password : password
    },
    header: {
      'content-type': 'application/json'
    },
    success: function(res) {
      if(res.statusCode == 200) {
        console.log('登录成功',res)
      } else {
        console.error('登录失败',res)
      }
    },
    fail: function(err) {
      console.error('网络请求出错',err)
    }
  })
}
// 加载问卷
export const loadSurverService =  (id) => {
  const app = getApp(); //获取小程序实例
  const token = app.globalData.accessToken||'';//直接从全局数据中获取token
  const headers = {
    'content-type': 'application/json',
    'token': token
  };
  try {
    const response =  new Promise((resolve, reject) => {
      wx.request({
        url:`http://202.192.19.106:8080/api/v2/survey/loadSurvey/${id}`,
        method: 'GET',
        header: headers,
        success: function(res) {
          resolve(res); // 成功时返回数据
          console.log(res)
        },
        fail: function(error) {
          reject(error); // 失败时抛出错误
        }
      });
    });
    return response;
  } catch (error) {
    console.error('请求失败：', error);
    throw error;
  }
};
// 提交问卷
export const userAnswerService = (data) => {
  const app = getApp(); //获取小程序实例
  const token = app.globalData.accessToken||''; //直接从全局数据中获取token
  const headers = {
    'content-type': 'application/json',
    'token': token
  };
  try {
    const response =  new Promise((resolve, reject) => {
      wx.request({
        url:'http://202.192.19.106:8080/api/v2/user-answer/create',
        method: 'POST',
        header: headers,
        data: data,
        success: function(res) {
          resolve(res); // 成功时返回数据
          // console.log(res)
        },
        fail: function(error) {
          reject(error); // 失败时抛出错误
        }
      });
    });
    return response;
  } catch (error) {
    console.error('请求失败：', error);
    throw error;
  }
}